import { defineStore } from "pinia";
import { UserState, UserPermission } from "@/stores/interface";
import piniaPersistConfig from "@/config/piniaPersist";
import { SystemApi } from "@/api/modules";

export const useUserStore = defineStore({
  id: "manage-user",
  state: (): UserState => ({
    token: "",
    userInfo: {
      createTime: "2023-04-23 16:11:38",
      updateBy: null,
      updateTime: null,
      remark: "管理员",
      userId: 1,
      deptId: 103,
      userName: "admin",
      nickName: "若依",
      email: "ry@163.com",
      phonenumber: "15888888888",
      sex: 1,
      avatar: "",
      password: "$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2",
      status: 0,
      delFlag: 0,
      loginIp: "175.8.67.29",
      loginDate: "2023-11-02T09:46:21.000+08:00",
      dept: {
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deptId: 103,
        parentId: 101,
        ancestors: "0,100,101",
        deptName: "研发部门",
        orderNum: 1,
        leader: "若依",
        phone: null,
        email: null,
        status: 0,
        delFlag: null,
        parentName: null,
        children: [],
      },
      roles: [
        {
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          roleId: 1,
          roleName: "超级管理员",
          roleKey: "admin",
          roleSort: 1,
          dataScope: 1,
          menuCheckStrictly: false,
          deptCheckStrictly: false,
          status: 0,
          delFlag: null,
          flag: false,
          menuIds: null,
          deptIds: null,
          permissions: null,
          admin: true,
        },
      ],
      roleIds: null,
      postIds: null,
      roleId: null,
      admin: true,
    },
    permissionInfo: {
      permissions: [],
      roles: [],
      user: {},
    },
  }),
  getters: {},
  actions: {
    // Set Token
    setToken(token: string) {
      this.token = token;
    },
    async getUserInfo() {
      const { data } = await SystemApi.getUserProfile();
      this.userInfo = data;
    },

    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      this.userInfo = userInfo;
    },
    setUserPermission(data: UserPermission) {
      this.userInfo = data.user;
      this.permissionInfo = data;
    },
  },
  persist: piniaPersistConfig("zj-manage-user"),
});
